package cn.hp.customer.controller;

import cn.hp.customer.pojo.TblSysUser;
import cn.hp.customer.service.AdminService;
import cn.hp.customer.util.ResponseData;
import cn.hp.customer.util.SysLog;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

// 管理模块
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService ;

    @RequestMapping("/registerPage")
    public String registerPage(){
        return "/WEB-INF/back/admin/register.html";
    }

    @RequestMapping("/loginPage")
    public String loginPage(){
        return "/WEB-INF/back/admin/admin.html";
    }

    // 账号密码的验证
    @RequestMapping("/login")
    @ResponseBody
    @SysLog(module = "后端管理" , operation = "管理员登录"  , method = "admin_login")
    public String login(TblSysUser user ,  HttpServletRequest request ){
        // 进行管理员的账号密码验证
        System.out.println("user:" + user);
        ResponseData    responseData = adminService.login(user) ;
        if (responseData.getCode() == 200 ){
            // 登录成功，将登录用户保存到session中
            request.getSession().setAttribute("adminName", user.getUsername() );
        }
        return JSON.toJSONString(responseData) ;
    }

    @ResponseBody
    @RequestMapping("/register")
    @SysLog(module = "后端管理" , operation = "管理员注册"  , method = "admin_register")
    public String register(TblSysUser user){
        ResponseData responseData  = adminService.register(user);
        return JSON.toJSONString(responseData) ;
    }
}
